package com.authine.cloudpivot.ext.utils.system;

import com.alibaba.cola.dto.RequestContext;
import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ServiceUrlConst;
import com.authine.cloudpivot.ext.dto.system.UserDto;
import com.authine.cloudpivot.ext.utils.HttpClients;
import com.authine.common.util.ObjectMapperUtils;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * @ClassName UserUtils
 * @Description: 用户工具类，方便调用
 * @Author fj
 * @Date 2021/8/20
 * @Version V1.0
 **/
public class UserUtils {

    /**
     * 根据登录id获取用户信息
     *
     * @return 用户信息
     */
    public static UserDto getCurrentUser() {
        return getUserByUserId(RequestContext.getCorpLoginId());
    }

    /**
     * 根据用户id获取用户信息
     *
     * @param userId 用户id
     * @return 用户信息
     */
    public static UserDto getUserByUserId(String userId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_USER_BY_ID_THROUGH_JDBC_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("id", userId);
        return HttpClients.execute(url, userMap, UserDto.class);
    }

    /**
     * 根据用户名获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    public static UserDto getUserByUsername(String username) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_USER_BY_USER_NAME_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("username", username);
        return HttpClients.execute(url, userMap, UserDto.class);
    }

    /**
     * 通过用户id判断当前用户是否存在
     *
     * @param userId 用户id
     * @return true：存在 false：不存在
     */
    public static boolean exists(String userId) {
        boolean result;
        try {
            // 用户id为空会抛出异常
            getUserByUserId(userId);
            result = true;
        } catch (BizException e) {
            result = false;
        }
        return result;
    }

    /**
     * 查询登录用户组织下最高管理员角色
     *
     * @return 用户信息
     */
    public static UserDto getOrgAdminUserByLoginId() {
        UserDto userDto = getCurrentUser();
        return getOrganizationAdminUser(userDto.getOrganization_id().get(0).getId());
    }

    /**
     * 查询指定用户组织下最高管理员角色
     *
     * @param orgId 组织id
     * @return 用户信息
     */
    public static UserDto getOrganizationAdminUser(String orgId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORGANIZATION_ADMIN_USER_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("id", orgId);
        return HttpClients.execute(url, userMap, UserDto.class);
    }

    /**
     * 根据角色名称查询用户组织角色下所有用户列表
     *
     * @param userId    用户id
     * @param roleName  角色名称
     * @return 用户信息
     */
    public static List<UserDto> getOrganizationUserByRoleName(String userId, String roleName) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_ORGANIZATION_USER_BY_ROLE_NAME_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("id", userId);
        userMap.put("roleName", roleName);
        String execute = HttpClients.execute(url, userMap);
        return ObjectMapperUtils.readValue(execute, UserDto.class);
    }

    /**
     * 根据部门管理员获取下属员工id列表,根据组织管理员获取组织所有员工id列表
     *
     * @param memberId    用户id
     * @return 用户id列表
     */
    public static List<String> listSubMemberIds(String memberId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.LIST_SUB_MEMBER_IDS_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("id", memberId);
        String execute = HttpClients.execute(url, userMap);
        return ObjectMapperUtils.readValue(execute, String.class);
    }

    /**
     * 获取组织内的用户ID列表
     *
     * @param orgIds    组织id列表
     * @return 用户id列表
     */
    public static List<String> listSubMemberIdsByOrganizationId(List<String> orgIds) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.LIST_SUB_MEMBER_IDS_BY_ORGANIZATION_ID_URL);
        Map<String, Object> userMap = Maps.newHashMap();
        userMap.put("ids", orgIds);
        String execute = HttpClients.execute(url, userMap);
        return ObjectMapperUtils.readValue(execute, String.class);
    }
}
